package app.fugema.shop.main.goods.vm;

import android.app.Application;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import app.fugema.shop.main.goods.model.CateGoodsPageBean;
import app.fugema.shop.main.goods.model.CategoryPageBean;
import app.fugema.shop.main.goods.model.GoodsDetailResult;
import app.fugema.shop.main.goods.model.GoodsPageBean;
import app.fugema.shop.main.goods.model.GoodsSearchParams;
import app.fugema.shop.main.goods.model.ShopCouponResult;
import app.fugema.shop.main.goods.model.SimilarGoodsResult;
import app.fugema.shop.main.goods.model.VShopResult;

import com.base.common.netBeanPackage.BaseApiResult;
import com.base.common.netBeanPackage.LiveDataWrapper;
import com.base.common.utils.UIUtils;
import com.base.common.viewmodel.BaseViewModel;
import app.fugema.shop.http.HttpService;
import app.fugema.shop.main.evaluate.model.EvaluatePageResult;
import app.fugema.shop.main.order.vm.GroupBookingVM;

import java.util.HashMap;
import java.util.Map;

public class GoodsVM extends BaseViewModel {

    private GroupBookingVM groupBookingVM;


    public GoodsVM(@NonNull Application application) {
        super(application);
        groupBookingVM = new GroupBookingVM(application);
        addBaseViewModel(groupBookingVM);
    }

    public GroupBookingVM getGroupBookingVM() {
        return groupBookingVM;
    }

    /**
     * 添加 或取消 收藏商品
     * @param goodsId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> addFavoriteProduct(int goodsId, String userKey)
    {
        return send(HttpService.getHttpServer().addFavoriteProduct(goodsId,userKey));
    }

    /**
     * 查询产品详情
     * @param pId
     * @return
     */
    public MutableLiveData<LiveDataWrapper<GoodsDetailResult>> getProductDetail(int pId ,String userKey)
    {
        Map<String,Object> idParams = new HashMap<>();
        idParams.put("id",pId);
        if( UIUtils.isNotEmpty( userKey ) )
        {
            idParams.put("userkey",userKey);
        }
        return send(HttpService.getHttpServer().getProductDetail(idParams));
    }

    /**
     * 新品推荐
     *
     * @param pn
     * @return
     */
    public MutableLiveData<LiveDataWrapper<GoodsPageBean>> getHomeGoodsByPage(int pn)
    {
        return send(HttpService.getHttpServer().getHomeGoodsByPage(pn, 10));
    }


    /**
     * 分类信息列表
     *
     * @param
     * @return
     */
    public MutableLiveData<LiveDataWrapper<CategoryPageBean>> categoryInfo()
    {
        return send(HttpService.getHttpServer().categoryInfo());
    }

    /**
     * 通过分类id商品列表信息
     *
     * @param
     * @param pn
     * @return
     */

    public MutableLiveData<LiveDataWrapper<CateGoodsPageBean>> getPageGoodsByCateId(int categoryId, int pn)
    {
        return send(HttpService.getHttpServer().getGoodsByCateId(categoryId, pn, 10));
    }

    public MutableLiveData<LiveDataWrapper<CateGoodsPageBean>> getPageGoodsByParams(GoodsSearchParams searchParams)
    {
        Map<String, Object> map = new HashMap<>();

        map.put("pageno", searchParams.getPageno());
        map.put("pagesize", searchParams.getPagesize());
        if( UIUtils.isNotEmpty(searchParams.getCid()) && searchParams.getCid() != 0  )
        {
            map.put("cid", searchParams.getCid());
            //long b_id = 0, /* 品牌ID */
        }
        if( UIUtils.isNotEmpty(searchParams.getOrderkey()) && searchParams.getOrderkey() != 0  )
        {
            //排序项（1：默认，2：销量，3：价格，4：评论数，5：上架时间）
            map.put("orderkey", searchParams.getOrderkey());
        }
        if( UIUtils.isNotEmpty(searchParams.getOrdertype()) && searchParams.getOrdertype() != 0  )
        {
            // 排序方式（1：升序，2：降序）
            map.put("ordertype", searchParams.getOrdertype());
        }
        if( UIUtils.isNotEmpty(searchParams.getKeywords()) )
        {
            map.put("keywords", searchParams.getKeywords());
        }
        if( UIUtils.isNotEmpty(searchParams.getExp_keywords()) )
        {
            map.put("exp_keywords", searchParams.getExp_keywords());
        }
        if( UIUtils.isNotEmpty(searchParams.getUserkey()) )
        {
            map.put("userkey", searchParams.getUserkey());
        }
        if( UIUtils.isNotEmpty(searchParams.getVshopid()) && searchParams.getVshopid() != 0 )
        {
            map.put("vshopid", searchParams.getVshopid());
        }
        return send(HttpService.getHttpServer().getGoodsByParam(map));
    }


    /**
     *  查询 改商品 第一条 评论
     * @param prodId
     * @return
     */
    public MutableLiveData<LiveDataWrapper<EvaluatePageResult>> getGoodsEvaluate(int prodId)
    {
         //{"success":false,"msg":"该宝贝暂无评论！"}
        return send(HttpService.getHttpServer().getGoodsEvaluateById(prodId));
    }

    /**
     *  领取优惠券
     * @param vshopId
     * @param couponId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> applyCoupon(int vshopId,int couponId,String userKey)
    {
        return send(HttpService.getHttpServer().applyCoupon(String.valueOf(vshopId),String.valueOf(couponId),userKey));
    }


    /**
     * 查询 微店详情 by ID
     * @param shopId
     * @return
     */
    public MutableLiveData<LiveDataWrapper<VShopResult>> getShopById(int shopId)
    {
        return send(HttpService.getHttpServer().getShopById(shopId));
    }

    /**
     * 添加 或取消 收藏的店铺
     * @param shopId
     * @param userKey
     * @return
     */
    public MutableLiveData<LiveDataWrapper<BaseApiResult>> addFavoriteShop(int shopId, String userKey)
    {
        return send(HttpService.getHttpServer().addFavaoriteShop(shopId,userKey));
    }

    /**
     *  获取店铺优惠券信息
     * @param shopId
     * @return
     */
    public MutableLiveData<LiveDataWrapper<ShopCouponResult>> getShopCouponListByShopId(int shopId ,String userKey)
    {

        Map<String,Object> shopParams = new HashMap<String,Object>();
        shopParams.put("shopId",shopId);
        if( UIUtils.isNotEmpty( userKey ) )
        {
            shopParams.put("userkey",userKey);
        }
        return send(HttpService.getHttpServer().getShopCouponListByShopId(shopParams));
    }


    /**
     *  关键词搜索
     * @param searchParams
     * @return
     */
    public MutableLiveData<LiveDataWrapper<CateGoodsPageBean>> searchProductByParams(GoodsSearchParams searchParams)
    {
        Map<String, Object> map = new HashMap<>();

        map.put("pageno", searchParams.getPageno());
        map.put("pagesize", searchParams.getPagesize());
        if( UIUtils.isNotEmpty(searchParams.getCid()) && searchParams.getCid() != 0  )
        {
            map.put("cid", searchParams.getCid());
            //long b_id = 0, /* 品牌ID */
        }
        if( UIUtils.isNotEmpty(searchParams.getOrderkey()) && searchParams.getOrderkey() != 0  )
        {
            //排序项（1：默认，2：销量，3：价格，4：评论数，5：上架时间）
            map.put("orderkey", searchParams.getOrderkey());
        }
        if( UIUtils.isNotEmpty(searchParams.getOrdertype()) && searchParams.getOrdertype() != 0  )
        {
            // 排序方式（1：升序，2：降序）
            map.put("ordertype", searchParams.getOrdertype());
        }
        if( UIUtils.isNotEmpty(searchParams.getKeywords()) )
        {
            map.put("keywords", searchParams.getKeywords());
        }
        if( UIUtils.isNotEmpty(searchParams.getExp_keywords()) )
        {
            map.put("exp_keywords", searchParams.getExp_keywords());
        }
        if( UIUtils.isNotEmpty(searchParams.getUserkey()) )
        {
            map.put("userkey", searchParams.getUserkey());
        }

        return send(HttpService.getHttpServer().searchProductFilter(map));
    }

    /**
     * 查询相似 商品
     * @param productId
     * @return
     */
    public MutableLiveData<LiveDataWrapper<SimilarGoodsResult>> getSimilarProduct(int productId, int pn)
    {
        return send(HttpService.getHttpServer().getSimilarProduct(productId,pn,10));
    }

    /**
     * 商品详情页 推荐商品
     * @param productId
     * @return
     */
    public MutableLiveData<LiveDataWrapper<SimilarGoodsResult>> getDetailSimilarProduct(int productId)
    {
        return send(HttpService.getHttpServer().getSimilarProduct(productId,1,6));
    }

}
